#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* rotateRight(struct ListNode* head, int k) {
    if (head == NULL || k == 0)
        return head;
    int l = 0;
    struct ListNode* ptail = head, * pend = NULL;
    while (ptail)
    {
        if (ptail->next == NULL)
            pend = ptail;
        ptail = ptail->next;
        l++;
    }
    int i = l - (k % l);
    ptail = head;
    while (--i)
    {
        ptail = ptail->next;
    }
    pend->next = head;
    struct ListNode* pret = ptail->next;
    ptail->next = NULL;
    return pret;
}